package com.hongkzh.www.adv.controller;

import com.auth0.jwt.interfaces.Claim;
import com.hongkzh.www.adv.service.AdvService;
import com.hongkzh.www.adv.service.CarouselService;
import com.hongkzh.www.adv.service.EnterpriseAdvService;
import com.hongkzh.www.conmon.BaseController;
import com.hongkzh.www.conmon.JwtUtil;
import com.hongkzh.www.conmon.ReqResponse;
import com.hongkzh.www.swagger.annotation.*;
import com.jfinal.aop.Clear;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;

import java.util.List;
import java.util.Map;

/**
 * @author martins
 */
@Api(tag = "enterpriseAdv", description = "企业广告")
public class EnterpriseAdvController extends BaseController {
    static EnterpriseAdvService enterpriseAdvService = new EnterpriseAdvService();
    static CarouselService carouselService = new CarouselService();
    static AdvService advService = new AdvService();

    @ApiOperation(url = "/v1/enterpriseAdv/getEnterpriseCarouselList", tag = "enterpriseAdv", httpMethod = "post", description = "获取企业广告轮播图")

    @ApiResponses({
            @ApiResponse(code = 200, message = "成功", responseHeaders = {
                    @ResponseHeader(name = "code", description = " 0成功 1失败"),
                    @ResponseHeader(name = "data", description = "{ imgSrc 图片地址 ,imgLinks 图片链接 imgRank 图片排序  }"),
                    @ResponseHeader(name = "msg", description = "失败原因")})

    })
    public void getEnterpriseCarouselList() {
        ReqResponse<List<Record>> r = new ReqResponse();
        List<Record> record = carouselService.getEnterpriseCarouselList();
        r.setData(record);
        renderJson(r);
    }

    @ApiOperation(url = "/v1/enterpriseAdv/getEnterpriseCategoryList", tag = "enterpriseAdv", httpMethod = "post", description = "获取企业广告分类列表")
    @ApiResponses({
            @ApiResponse(code = 200, message = "成功", responseHeaders = {
                    @ResponseHeader(name = "code", description = " 0成功 1失败"),
                    @ResponseHeader(name = "data", description = "{   name 标题   id 类别id labelColor 颜色值}"),
                    @ResponseHeader(name = "msg", description = "失败原因")})
    })
    public void getEnterpriseCategoryList() {
        ReqResponse<List<Record>> r = new ReqResponse();
        List<Record> record = enterpriseAdvService.getEnterpriseCategoryList();
        r.setData(record);
        renderJson(r);
    }

    @ApiOperation(url = "/v1/enterpriseAdv/getEnterpriseListByCategory", tag = "enterpriseAdv", httpMethod = "post", description = "获取企业广告分类数据")
    @Params({

            @Param(name = "categoryId", description = "分类id", required = false, dataType = "string"),
            @Param(name = "pageNumber", description = "页码", required = true, dataType = "string")
    })
    @ApiResponses({
            @ApiResponse(code = 200, message = "成功", responseHeaders = {
                    @ResponseHeader(name = "code", description = " 0成功 1失败"),
                    @ResponseHeader(name = "data", description = "totalRow 总行数  pageNumber第几页 firstPage 是否第一页 lastPage 是否最后一页 totalPage 总页数 pageSize每页条数 " +
                            " list[{vedioLength 视频长度   coverImgSrc 图片地址   imgRank 图片排序 title 标题  playCount 观看总数   uid 用户id  uName 用户名称 headImg  用户头像   " +
                            "playCount 观看总数   costCount 消费总数 integralCount 总积分数 lastCount 剩余积分 percentage 百分比}]"),
                    @ResponseHeader(name = "msg", description = "失败原因")})
    })
    public void getEnterpriseListByCategory() {
        String categoryId = getPara("categoryId");
        int pageNumber = getParaToInt("pageNumber");
        ReqResponse<Page<Record>> r = new ReqResponse();
        Page<Record> record = enterpriseAdvService.getEnterpriseListByCategory(categoryId, pageNumber);
        r.setData(record);
        renderJson(r);
    }

    @ApiOperation(url = "/v1/enterpriseAdv/getEnterpriseHotAdvTypeList", tag = "enterpriseAdv", httpMethod = "post", description = "获取企业广告热度类型列表")
    @ApiResponses({
            @ApiResponse(code = 200, message = "成功", responseHeaders = {
                    @ResponseHeader(name = "code", description = " 0成功 1失败"),
                    @ResponseHeader(name = "data", description = "{   title 标题 subtitle 副标题 typeId 热度类型id   currentTime 当前时间 beginTime 开始时间 endTime 结束时间 beginDate 开始时间 endDate 结束时间 sortDate -1未开始，0进行中，1已结束}"),
                    @ResponseHeader(name = "msg", description = "失败原因")})
    })
    public void getEnterpriseHotAdvTypeList() {
        ReqResponse<List<Record>> r = new ReqResponse();
        List<Record> record = enterpriseAdvService.getEnterpriseHotAdvTypeList();
        r.setData(record);
        renderJson(r);
    }

    @ApiOperation(url = "/v1/enterpriseAdv/getEnterpriseHotAdvList", tag = "enterpriseAdv", httpMethod = "post", description = "获取企业广告热度列表")
    @Params({
            @Param(name = "typeId", description = "企业广告热度类型id", required = true, dataType = "string"),
            @Param(name = "pageNumber", description = "页码", required = true, dataType = "string")
    })
    @ApiResponses({
            @ApiResponse(code = 200, message = "成功", responseHeaders = {
                    @ResponseHeader(name = "code", description = " 0成功 1失败"),
                    @ResponseHeader(name = "data", description = "totalRow 总行数  pageNumber第几页 firstPage 是否第一页 lastPage 是否最后一页 totalPage 总页数 pageSize每页条数  list[{vedioLength 视频长度 showType 1视频 2网页 content 网页内容 coverImgSrc 图片地址   imgRank 图片排序 title 标题  playCount 观看总数   costCount 消费总数 integralCount 总积分数 lastCount 剩余积分 percentage 百分比 uid 用户id  uName 用户名称 headImg  用户头像   }]"),
                    @ResponseHeader(name = "msg", description = "失败原因")})
    })

    public void getEnterpriseHotAdvList() {
        String id = getPara("typeId");
        int pageNumber = getParaToInt("pageNumber");
        ReqResponse<Page<Record>> r = new ReqResponse();
        Page<Record> record = enterpriseAdvService.getEnterpriseHotAdvList(id,pageNumber);
        r.setData(record);
        renderJson(r);
    }



/*
    @ApiOperation(url = "/v1/enterpriseAdv/getEnterpriseCategoryAdvList", tag = "enterpriseAdv", httpMethod = "post",description = "获取企业广告具体分类页  列表")
    @Params({
            @Param(name = "id", description = "分类id", required = true, dataType = "string"),
            @Param(name = "pageNumber", description = "页码", required = true, dataType = "string"),
            @Param(name = "sortId", description = "排序字段 默认排序 rank  最多浏览 play_count  最新上传 create_date ", required = false, dataType = "string"),
            @Param(name = "sortValue", description = "排序值 asc 升 desc 降 ", required = false, dataType = "string")
    })
    @ApiResponses({
            @ApiResponse(code=200,message="成功",responseHeaders={
                    @ResponseHeader(name="code",description = " 0成功 1失败"),
                    @ResponseHeader(name="data",description = "totalRow 总行数  pageNumber第几页 firstPage 是否第一页 lastPage 是否最后一页 totalPage 总页数 pageSize每页条数  list[{ coverImgSrc 图片地址 coverLink 图片链接  imgSrc 视频地址 ,imgLinks 视频链接 imgRank 图片排序 title 标题 imgNote 副标题   headImg 头像   uid 用户id  uName 用户名 shareCount分享总数  praiseCount点赞总数 }]"),
                    @ResponseHeader(name="msg",description = "失败原因") })

    })
    public void getEnterpriseCategoryAdvList(){
        String cid=getPara("id");
        int pageNumber=getParaToInt("pageNumber");
        String sortId=getPara("sortId");
        String sortValue=getPara("sortValue");
        ReqResponse<Page<Record>> r=new ReqResponse();
        Page<Record> record= enterpriseAdvService.getEnterpriseCategoryAdvList(cid,pageNumber,sortId,sortValue);
        r.setData(record);
        renderJson(r);
    }*/

    @ApiOperation(url = "/v1/enterpriseAdv/getEnterpriseAdvById", tag = "enterpriseAdv", httpMethod = "post", description = "获取企业广告视频信息")
    @Params({
            @Param(name = "loginUid", description = "当前登录用户id", required = false, dataType = "string"),
            @Param(name = "id", description = "视频id", required = true, dataType = "string")
    })
    @ApiResponses({
            @ApiResponse(code = 200, message = "成功", responseHeaders = {
                    @ResponseHeader(name = "code", description = " 0成功 1失败"),
                    @ResponseHeader(name = "data", description = "  {historyId 前一个历史播放记录 " +
                            "enterpriseId 企业id  playCount 播放总数 praiseCount 点赞总数 follows 关注数 " +
                            "collectionState 已经收藏 1   没收藏 0 praiseState 已经点赞 1 " +
                            " integra 单个奖励数  integralCount 总数  " +
                            " 没点赞 0  uid 用户id uName  用户名称 headImg 用户头像  coverImgSrc " +
                            "图片地址 coverLink 图片链接  imgSrc 视频地址 ,imgLinks 视频链接 imgRank " +
                            "图片排序 title 标题 imgNote 副标题  type 类型  1自媒体 2企业广告 3城市广告 4传统文化  " +
                            "createDate 创建时间 circle 圈子数 officiaVideo 官方视频 officiaImgSrc 官方视频封面" +
                            "products[imgSrc 图片 title 标题   ] " + "historyIdState 为空 用户未领取过红包 不为空 用户已领取" + "lastCount 剩余数" +
                            "} "),
                    @ResponseHeader(name = "msg", description = "失败原因")})

    })

    @Clear
    public void getEnterpriseAdvById() {
        String token = getPara("loginUid");
        String loginUid = "";
        if (StrKit.notBlank(token)) {
            Map<String, Claim> map = JwtUtil.unsign(token);
            loginUid = map.get("user_id").asString();
        }
        String id = getPara("id");
        ReqResponse<Record> r = new ReqResponse();

        Record record = advService.getAdvById(id, loginUid);

        if (null != record) {
            record.set("recommends", null);
            List<Record> recommends = advService.getAdvRecommendByAdvId(id);
            if (null != recommends) {
                record.set("recommends", recommends);
            }
            record.set("products", null);
            List<Record> products = advService.getAdvProductByAdvId(id);
            if (null != products) {
                record.set("products", products);
            }

            record.set("historyId", "");
            Record history = advService.historyEnterpriseAdv(id, loginUid);
            if (null != history) {
                record.set("historyId", history.get("id"));
            }
            r.setData(record);
        } else {
            r.setCode(1);
            r.setMsg("网络异常");
        }

        renderJson(r);
    }

    @ApiOperation(url = "/v1/enterpriseAdv/getEnterpriseId", tag = "enterpriseAdv", httpMethod = "post", description = "获取企业信息")
    @Params({
            @Param(name = "loginUid", description = "当前登录用户id", required = true, dataType = "string"),
            @Param(name = "enterpriseId", description = "企业id", required = true, dataType = "string")
    })
    @ApiResponses({
            @ApiResponse(code = 200, message = "成功", responseHeaders = {
                    @ResponseHeader(name = "code", description = " 0成功 1失败"),
                    @ResponseHeader(name = "data", description = " followState 1关注0否 follows关注数 eName 企业名称enterpriseId 企业id headImg头像 introduce简介"),
                    @ResponseHeader(name = "msg", description = "失败原因")})

    })
    public void getEnterpriseId() {
        String loginUid = getAttr("loginUid");
        ReqResponse<Record> r = new ReqResponse();
        String enterpriseId = getPara("enterpriseId");
        Record record = advService.getEnterpriseId(enterpriseId, loginUid);
        r.setData(record);
        renderJson(r);
    }

    @ApiOperation(url = "/v1/enterpriseAdv/getEnterpriseReleaseAdvList", tag = "enterpriseAdv", httpMethod = "post", description = "获取企业历史发布列表")
    @Params({
            @Param(name = "enterpriseId", description = "企业id", required = true, dataType = "string"),
            @Param(name = "pageNumber", description = "页码", required = true, dataType = "string")
    })
    @ApiResponses({
            @ApiResponse(code = 200, message = "成功", responseHeaders = {
                    @ResponseHeader(name = "code", description = " 0成功 1失败"),
                    @ResponseHeader(name = "data", description = "totalRow 总行数  pageNumber第几页 firstPage 是否第一页 lastPage 是否最后一页 totalPage 总页数 pageSize每页条数  " +
                            "list[{vedioLength 视频长度   coverImgSrc 图片地址    title 标题  playCount 观看总数    integral  总积分数   advId  createDate 创建时间  }]"),
                    @ResponseHeader(name = "msg", description = "失败原因")})
    })
    public void getEnterpriseReleaseAdvList() {
        String enterpriseId = getPara("enterpriseId");
        int pageNumber = getParaToInt("pageNumber");
        ReqResponse<Page<Record>> r = new ReqResponse();
        Page<Record> record = enterpriseAdvService.getEnterpriseReleaseAdvList(enterpriseId, pageNumber);
        r.setData(record);
        renderJson(r);
    }

    @ApiOperation(url = "/v1/enterpriseAdv/collection", tag = "enterpriseAdv", httpMethod = "post", description = "通过用户id   视频id  state收藏状态 1收藏0取消收藏")
    @Params({
            @Param(name = "loginUid", description = "当前用户id", required = true, dataType = "string"),
            @Param(name = "state", description = "收藏状态 1收藏0取消收藏", required = true, dataType = "string"),
            @Param(name = "id", description = "视频id", required = true, dataType = "string")
    })
    @ApiResponses({
            @ApiResponse(code = 200, message = "成功", responseHeaders = {
                    @ResponseHeader(name = "data", description = " 总收藏数"),
                    @ResponseHeader(name = "code", description = " 0成功 1失败"),
                    @ResponseHeader(name = "msg", description = "失败原因")})

    })

    public void collection() {
        EnterpriseAdvService enterpriseAdvService2 = enhance(EnterpriseAdvService.class);
        String loginUid = getAttr("loginUid");
        String id = getPara("id");
        String state = getPara("state");
        ReqResponse<String> r = new ReqResponse();
        try {
            r.setData(enterpriseAdvService2.collection(loginUid, id, state));

        } catch (Exception e) {
            e.printStackTrace();
            r.setCode(1);
            r.setMsg("操作失败");
        }
        renderJson(r);
    }

    @ApiOperation(url = "/v1/enterpriseAdv/praise", tag = "enterpriseAdv", httpMethod = "post", description = "通过用户id   视频id  state点赞状态 1点赞0取消点赞")
    @Params({
            @Param(name = "loginUid", description = "当前用户id", required = true, dataType = "string"),
            @Param(name = "state", description = "点赞状态 1点赞0取消点赞", required = true, dataType = "string"),
            @Param(name = "id", description = "视频id", required = true, dataType = "string")
    })
    @ApiResponses({
            @ApiResponse(code = 200, message = "成功", responseHeaders = {
                    @ResponseHeader(name = "data", description = " 总点赞数"),
                    @ResponseHeader(name = "code", description = " 0成功 1失败"),
                    @ResponseHeader(name = "msg", description = "失败原因")})

    })

    public void praise() {
        EnterpriseAdvService enterpriseAdvService2 = enhance(EnterpriseAdvService.class);
        String loginUid = getAttr("loginUid");
        String id = getPara("id");
        String state = getPara("state");
        ReqResponse<String> r = new ReqResponse();
        try {
            r.setData(enterpriseAdvService2.praise(loginUid, id, state));

        } catch (Exception e) {
            e.printStackTrace();
            r.setCode(1);
            r.setMsg("操作失败");
        }
        renderJson(r);
    }

    @ApiOperation(url = "/v1/enterpriseAdv/endPlayEnterpriseAdvById", tag = "enterpriseAdv", httpMethod = "post", description = "播放完成给用户的积分")
    @Params({
            @Param(name = "id", description = "视频id", required = true, dataType = "string"),
            @Param(name = "loginUid", description = "当前用户id", required = true, dataType = "string"),

    })
    @ApiResponses({
            @ApiResponse(code = 200, message = "成功", responseHeaders = {
                    @ResponseHeader(name = "code", description = " 0成功 1失败"),
                    @ResponseHeader(name = "data", description = " type   1 乐豆 2乐宝 3乐币 integral 数量 "),
                    @ResponseHeader(name = "msg", description = "失败原因")})

    })
    public void endPlayEnterpriseAdvById() {
        AdvService advService2 = enhance(AdvService.class);
//        String loginUid = getAttr("loginUid");
        String loginUid = getPara("loginUid");
        String id = getPara("id");
        ReqResponse<Record> r = new ReqResponse();
        try {
            Record record = advService2.endPlayEnterpriseAdvById(loginUid, id);

            r.setData(record);
        } catch (Exception e) {
            e.printStackTrace();
            r.setCode(1);

        }
        renderJson(r);
    }

}
